﻿using System.Diagnostics.Contracts;
using Microsoft.Pex.Framework;

namespace AutomaTones.Model.MIDI
{
    /// <summary>
    /// Class the can translate a musical format of time (Beats Per Minute) to milliseconds used by Threads.
    /// Instanciate with a BPM and then call the methods to get the corrects value in milliseconds for the called subdivision.
    /// </summary>
    /// <author>Anders Bech Mellson, anbh@itu.dk</author>
    [PexClass]
    public class Tempo {
        private readonly int _bpm;

        /// <summary>
        /// Creates a tempo object with the desired tempo in Beats per minute.
        /// </summary>
        /// <param name="BPM"></param>
        public Tempo(int BPM) {
            Contract.Requires(BPM>0);
            this._bpm = BPM;
        }

        /// <summary>
        /// Return the current BPM.
        /// </summary>
        [PexMethod]
        public int BPM() {
            return _bpm;
        }

        /// <summary>
        /// Returns the value of a wholenote in milliseconds.
        /// </summary>
        [PexMethod]
        public int WholeNote() {
            return 240000/_bpm;
        }

        /// <summary>
        /// Returns the value of a halfnote in milliseconds.
        /// </summary>
        [PexMethod]
        public int HalfNote() {
            return 120000/_bpm;
        }

        /// <summary>
        /// Returns the value of a quarternote in milliseconds.
        /// </summary>
        [PexMethod]
        public int QuarterNote() {
            return 60000/_bpm;
        }

        /// <summary>
        /// Returns the value of a eightnote in milliseconds.
        /// </summary>
        [PexMethod]
        public int EighthNote() {
            return 30000/_bpm;
        }

        /// <summary>
        /// Returns the value of a sixteenthnote in milliseconds.
        /// </summary>
        [PexMethod]
        public int SixteenthNote() {
            return 15000/_bpm;
        }

        /// <summary>
        /// Returns the value of a thirtysecondnote in milliseconds.
        /// </summary>
        [PexMethod]
        public int ThirtySecondNote() {
            return 7500/_bpm;
        }
    }
}
